<?php

	function vnnews_display_featured_categories($cat_id, $view_type)
	{
		//$view_type == 52 -- 4 left and 1 right
		// set to $view_type = 5
		$count = $view_type == 52 ? 5 : $view_type;

		if(empty($cat_id))
			return '';

		$str = "<div class='cat-header'>";

		$cat_name = get_cat_name( $cat_id );
		$cat_link = get_category_link($cat_id );

		$str .= "<h4><a href='". esc_url($cat_link) . "'>". $cat_name ."</a></h4>";
		$str .= "</div>";

		$args = array(
					'cat' => $cat_id,
					'post_status' => 'publish',
					'ignore_sticky_posts' => 1,
					'posts_per_page' => $count
				);

		$query = new WP_Query( $args );

		if ( $query -> have_posts() ) :

			while ( $query -> have_posts() ) : $query -> the_post();

				$post_id = get_the_ID();
				$current_post = $query->current_post;
				$last_post = $query->post_count - 1;
				$link = get_permalink();
				$title = the_title('','',FALSE);
				$short_title = $title;//mb_substr($title, 0, 60);
				//if (strlen($title) > 60){
					//$short_title .= ' ...';
				//}
				$featurevid = get_post_meta($post_id, 'featurevid',true);

				$num_comments = get_comments_number($post_id);

				$meta = "";
				$meta .= '<div class="entry-meta">';
				$meta .= '<span class="date">';
				$meta .= '<i class="icon-calendar"></i>';
				$meta .= get_the_date();
				$meta .= '</span>';

				if (($view_type == 5 && $current_post == 0) || ($view_type == 52 && $current_post == $last_post)) {
					$meta .= '<span class="category">';
					$meta .= '<i class="icon-folder-close"></i>';
					$meta .= '<a href="'. $cat_link .'">'. $cat_name .'</a>';
					$meta .= '</span>';

					$meta .= '<span class="views">';
					$meta .= '<i class="icon-eye-open"></i>';
					$meta .= getPostViews($post_id) . ' views';
					$meta .= '</span>';
				}

				if (($view_type != 5 && $view_type != 52) || ($view_type == 5 && $current_post != 0) || ($view_type == 52 && $current_post != $last_post)){
					$comments = $num_comments . __(' comments');
					$write_comments = '<span class="comments">';
					$write_comments .= '<i class="icon-comments"></i>';
					$write_comments .= '<a href="' . get_comments_link() .'">'. $comments.'</a></span>';
					$meta .= $write_comments;
				}

				$meta .= '</div>';

				if ( $view_type == 5 || $view_type == 52):

					if(($view_type == 5 && $current_post == 0) || ($view_type == 52 && $current_post == $last_post)):
						$str .= '<div class="eight columns">';
						$str .=	$view_type == 5 ? '<div class="column-left">' : '<div class="column-right">';
						$str .= '<div class="main-post">';

						if ( has_post_thumbnail() || !empty($featurevid) ) {

							if ( !empty($featurevid) ) {
								$str .= '<div class="feature-vid">'. $featurevid .'</div>';
							} else {
								$str .= '<a href="'. $link .'" rel="bookmark" title="'. $title .'">';
								$str .=  get_the_post_thumbnail( $post_id, array(360,202 ) );
								$str .= '</a>';
							}

						}

						$str .= '<div class="post-details">';
						$str .= '<h3>';
						$str .= '<a href="'. $link .'" title = "'. $title.'">'. $short_title .'</a>';
						$str .= '</h3>';

						$str .= $meta;

						$str .= get_the_excerpt();

						$str .= "</div></div></div></div>";

					else:
						$str .= ($view_type == 5 && $current_post == 1) ? '<div class="eight columns"><div class="column-right"><div class="post-list">' : "";
						$str .= ($view_type == 52 && $current_post == 0) ? '<div class="eight columns"><div class="column-left"><div class="post-list">' : "";
						$str .= '<div class="post-item">';
						if ( has_post_thumbnail() ) {
							$str .= '<div class="thumb">';
							$str .= '<a href="'. $link .'">'. get_the_post_thumbnail($post_id, array(75,75) ) . '</a>';
							$str .= '</div>';
						}

						$str .= '<div class="post-right">';
						$str .= '<h5><a href="'. $link .'" rel="bookmark">'. $short_title .'</a></h5>';
						$str .= '</div>';

						$str .= $meta;

						$str .= '</div>';
						$str .= ($view_type == 5 && $current_post == $last_post) ? "</div></div></div>" : "";
						$str .= ($view_type == 52 && $current_post == $last_post - 1) ? "</div></div></div>" : "";
					endif;

				else:
					$str .= $current_post == 0 ? '<div class="eight columns"><div class="column-left"><div class="post-list">' : "";
					$str .= $current_post == ($view_type / 2) ? '</div></div></div><div class="eight columns"><div class="column-right"><div class="post-list">' : "";
					$str .= '<div class="post-item">';
					if ( has_post_thumbnail() ) {
						$str .= '<div class="thumb">';
						$str .= '<a href="'. $link .'">'. get_the_post_thumbnail($post_id, array(75,75) ) . '</a>';
						$str .= '</div>';
					}

					$str .= '<div class="post-right">';
					$str .= '<h5><a href="'. $link .'" rel="bookmark">'. $short_title .'</a></h5>';
					$str .= '</div>';

					$str .= $meta;

					$str .= '</div>';
					$str .= $current_post == $last_post ? "</div></div></div>" : "";

				endif;

			endwhile;

		endif;

		wp_reset_postdata();

		return $str;
	}

	$featured_category_1 = of_get_option("featured_category_1");
	$view_type_featured_category_1 = of_get_option("view_type_featured_category_1");

?>

<div class="row">
	<?php
		for ($i= 1; $i < 7 ; $i++) {
			$featured_category = of_get_option("featured_category_". $i);
			$view_type_featured_category = of_get_option("view_type_featured_category_". $i);

			echo '<div class="sixteen columns">';
			echo vnnews_display_featured_categories($featured_category, $view_type_featured_category);
			echo '</div>';
		}
	?>
</div>